package com.itheima.service.impl;

import com.itheima.mapper.DeptMapperJob;
import com.itheima.mapper.EepMapperJob;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptServiceJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * CTassName: DeptServiceImplJob
 * Package: com.itheima.service.impl
 * Description:
 *
 * @Author xw
 * @Create 2023/11/4 19:58
 * @Version 1.0
 */

@Service
public class DeptServiceImplJob implements DeptServiceJob {


    @Autowired
    private DeptMapperJob deptMapperJob;

    @Autowired
    private EepMapperJob empMapper;

    // 查询
    @Override
    public Dept getNameList(Integer id) {
        //  调用方法
        Dept list = deptMapperJob.listName(id);
        return list;
    }

    // 修改
    @Override
    public Boolean update(Dept deptRequest) {

        Dept list = deptMapperJob.listName(deptRequest.getId());
        System.out.println(list);
        if (list == null) {
            return false;
        }

        // 跟新修改时间
        deptRequest.setUpdateTime(LocalDateTime.now());
        deptMapperJob.updateService(deptRequest);
        return true;

    }

    // 查询
    @Override
    public List<Dept> list() {

        List<Dept> list = deptMapperJob.list();
        return list;
    }

    // id 删除 部门（删除员工）
    @Transactional  // 开启事物
    @Override
    public void delete(Integer id) {
        deptMapperJob.delete(id);
        // 删除员工
        // int a = 1 / 0;
        empMapper.delete(id);
    }


    // 添加
    @Override
    public void addDept(Dept dept) {

        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());


        deptMapperJob.addDept(dept);


    }


}
